#!/usr/bin/env python
# encoding: utf-8
# @author: Lin Han
# @contact: voldemort22@126.com
# @file: delete_01_from_06.py
# @time: 2021/5/24 10:14
# @desc:
# 处理一下那些绑定了历史夹具的场景，有点场景绑定的数据里没有对应的历史夹具。
from .utils.mongo_tools import retrieve_all_mongo_values, update_mongo_value, create_one_mongo_value
from .views.configs import SCENE_COLLECTION, HISTORY_CLAMP_COLLECTION

scenes = retrieve_all_mongo_values({}, collection_name=SCENE_COLLECTION)
history_clamps = retrieve_all_mongo_values({}, collection_name=HISTORY_CLAMP_COLLECTION)
history_clamp_numbers = [i['number'] for i in history_clamps]

for i in scenes:
    if 'historyClamp' not in i.keys() or i['historyClamp'] is None:
        print(i)
        i['historyClamp'] = []
    for j in i['historyClamp']:
        if j in history_clamp_numbers:
            continue
        else:
            i['historyClamp'].remove(j)
    i['historyClamp'] = list(set(i['historyClamp']))
    update_mongo_value({"number": i["number"]}, new_value={"$set": i}, collection_name=SCENE_COLLECTION)

scene_15 = {
    "number": "2021060000016017",
    "name": "15",
    "type": "6",
    "desc": "三个固定支撑",
    "historyClamp": [
        "2021010000000357",
        "2021011000001498",
        "2021011000000175",
        "2021011000001601",
        "2021011000000294",
        "2021011000003106",
        "2021011000002335",
        "2021011000001548",
        "2021011000000675",
        "2021011000003234",
        "2021011000003731",
        "2021011000003302",
        "2021011000000891",
        "2021010000000142",
        "2021011000002078"
    ],
    "time": "2021-04-08 18:01",
    "standardClamp": []
}
create_one_mongo_value(scene_15, collection_name=SCENE_COLLECTION)

for i in scenes:
    if i['name'] == "15":
        scene_15 = i
    if i['name'] == "41":
        scene_41 = i
    if i['name'] == "18":
        scene_18 = i
    if i['name'] == "49":
        scene_49 = i

scene_18["historyClamp"].extend(scene_49["historyClamp"])
scene_18["historyClamp"] = list(set(scene_18["historyClamp"]))

scene_41["historyClamp"].extend(scene_15["historyClamp"])
scene_41["historyClamp"] = list(set(scene_41["historyClamp"]))

for i in scenes:
    if i['name'] == "41":
        i = scene_41
    if i['name'] == "18":
        i = scene_18
    update_mongo_value({"number": i["number"]}, new_value={"$set": i}, collection_name=SCENE_COLLECTION)
